﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.DataReports
{
    public partial class WorkOrderByDateReport : System.Web.UI.Page
    {
      

        protected void Page_Load(object sender, EventArgs e)
        {            
            // security - if the user is not logged in than he should not be here.
            if (!IsPostBack)
            {
                // checking logged in user
                if (clsUser.CurrentUserRoleId < 0)
                {
                    Response.RedirectTo("/LTR/Login.aspx?ReturnTo=" + Request.Url.ToString());
                }

                // direction
                if (SessParam.Culture == "he")
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Rtl_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.RightToLeft;
                }
                else
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Ltr_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.LeftToRight;
                }
                               
                DemoPanel.Visible = (Session["UserId"].ToString() == "39");
            }
        }

        protected void ddlOrgs_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlOrgs.Items.Clear();
                ListItem all = new ListItem("All", "0");
                ddlOrgs.Items.Add(all);
                ddlOrgs.AppendDataBoundItems = true;

                if (SessParam.IsClient)
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
                else
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
            }
        }

        protected void GenerateReport(object sender, EventArgs e)
        {
            List<TableOrganization> orgList = new List<TableOrganization>();
            int OrgId = int.Parse(ddlOrgs.SelectedValue);

            if (OrgId == 0)
            {
                OrgListPanel.Visible = true;
                if (SessParam.IsClient)
                {
                    orgList = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                    
                }
                else
                {
                    orgList = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);                    
                }
            }
            else
            {
                orgList.Add(MLY.Bll.clsOrganization.GetOrganizationById(OrgId));
                OrgListPanel.Visible = false;
            }
            
            ReportTablesPanel.Visible = true;
            if (OrgListPanel.Visible)
            {
                ReportOrganizationsAnchorsRepeater.DataSource = orgList;
                ReportOrganizationsAnchorsRepeater.DataBind();
            }
            FullReportRepeater.DataSource = orgList;
            FullReportRepeater.DataBind();
        }

        protected void OnFullRepeaterItemCreated(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                string OrdId = (e.Item.FindControl("OrganizationIdHiddenField") as HiddenField).Value;
                string CustomerId;
                string ContructorId;
                if (SessParam.IsClient)
                {
                    CustomerId = Session["OrganizationId"].ToString();
                    ContructorId = OrdId;
                }
                else
                {
                    CustomerId = OrdId;
                    ContructorId = Session["OrganizationId"].ToString();
                }
                DateTime From = DateTime.Parse(txtFromDate.Text.Trim());
                DateTime Till = DateTime.Parse(txtToDate.Text.Trim());

                GridView WOGrid = (e.Item.FindControl("ActualReportGridview") as GridView);
                WOGrid.DataSource = MLY.Bll.ReportsGenerator.WorkordersByDate(CustomerId, ContructorId, From, Till, ddlLateOrderColumnDate.SelectedValue);
                WOGrid.DataBind();
            }
        }

        protected string FormatDate(object Date)
        {
            string RetValue;

            try
            {
                RetValue = string.Format("{0:d}", DateTime.Parse(Date.ToString()));
            }
            catch
            {
                RetValue = ("");
            }
            return RetValue;


        }
    }
}
